package com.hyt.it.ogt.pj.data.utils;

import java.util.ArrayList;
import java.util.List;

import com.hyt.it.ogt.pj.data.common.Constants;
import com.hyt.it.ogt.pj.data.model.vo.pt.MultiTree;

/**
 * @author zhang_xiang
 * @date 2021/12/01 10:06
 */
public class BuildTree {

	public static <T> MultiTree<T> buildName(List<MultiTree<T>> nodes) {
		if (nodes == null) {
			return null;
		}
		List<MultiTree<T>> topNodes = new ArrayList<>();
		for (MultiTree<T> children : nodes) {
			String pid = children.getParentId();
			if (pid == null || Constants.TREE_ROOT.toString().equals(pid)) {
				topNodes.add(children);
				continue;
			}
			for (MultiTree<T> parent : nodes) {
				String id = parent.getExaminationId();
				if (id != null && id.equals(pid)) {
					parent.getChildren().add(children);
					continue;
				}
			}
		}
		MultiTree<T> root = new MultiTree<>();
		root.setId(Constants.TREE_ROOT.toString());
		root.setParentId("");
		root.setParentName("");
		root.setChildren(topNodes);
		return root;
	}

}